from elasticsearch import Elasticsearch

# 连接到 Elasticsearch
es = Elasticsearch("http://127.0.0.1:9200/", timeout=30)

# 定义索引名称
# index_name = "myproject"  # 创建索引
# if not es.indices.exists(index=index_name):
#     es.indices.create(index=index_name)
#     print("索引创建成功")
# else:
#     print("索引已存在")


# es.index(index='myproject', body={
#     'id': 1,
#     'table_name': 'myproject',
#     'name': "小刘",
#     'type': 'username'
# })
#
# dsl = {
#     "query": {
#         "match_all": {}
#     }
# }
# res = es.search(index="myproject", body=dsl)
# print(res['hits']['hits'])


# response = es.delete_by_query(
#     index="myproject",
#     body={
#         "query": {
#             "match": {
#                 # "type": "cate"
#                 # "type": "homepage"
#                 # "type": "good"
#                 # "type": "combo"
#             }
#         }
#     }
# )
# print(response)


# index_name = 'myproject'
#
# # 构建查询体
# query_body = {
#     "query": {
#         "bool": {
#             "filter": {
#                 "term": {
#                     "type": "good"
#                 }
#             }
#         }
#     }
# }
#
# response = es.search(index=index_name, body=query_body)
# for hit in response['hits']['hits']:
#     print(hit['_source'])


